package com.zhishu.snatch.system.h2;

import java.io.File;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.h2.tools.Server;

public class H2Server {
	private static Logger logger = Logger.getLogger(H2Server.class);
	private static Server h2Server;

	public static void startH2Server() {
		if (h2Server != null && h2Server.isRunning(true)) {
			return;
		}
		try {
			logger.info("database path is " + "正在启动h2...");
			boolean dbIsActive = new File("h2db").isDirectory();
			logger.info("database path is " + new File("urls").getAbsolutePath());
			h2Server = Server.createPgServer(new String[] { "-baseDir", "urls" }).start();
		} catch (SQLException e) {
			logger.error("启动h2出错：" + e.toString());
			e.printStackTrace();
			throw new RuntimeException(e);
		}
		logger.info("H2数据库启动完毕...");
	}

	public static void stopH2Server() {
		if (h2Server != null) {
			logger.info("正在关闭h2...");
			h2Server.stop();
			logger.info("关闭成功.");
		}
	}
}
